package com.chhuang.ewcrm.config;

import com.chhuang.ewcrm.consts.EwcrmConstants;
import com.chhuang.ewcrm.filter.WebSiteMeshFilter;
import com.chhuang.ewcrm.interceptor.LoginInterceptor;
import com.chhuang.ewcrm.interceptor.WLoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    EwcrmProperties ewcrmProperties;

    @Bean
    public FilterRegistrationBean<WebSiteMeshFilter> siteMeshFilter() {
        FilterRegistrationBean<WebSiteMeshFilter> fitlerBean = new FilterRegistrationBean<WebSiteMeshFilter>();
        WebSiteMeshFilter siteMeshFilter = new WebSiteMeshFilter();
        fitlerBean.setFilter(siteMeshFilter);
        return fitlerBean;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //映射图片保存地址
        registry.addResourceHandler(EwcrmConstants.UPLOAD_URL+"**")
                .addResourceLocations("file:"+ewcrmProperties.getUploadPath());
    }

    @Bean
    public LoginInterceptor getLoginInterceptor(){
        return new LoginInterceptor();
    }
    @Bean
    public WLoginInterceptor getWLoginInterceptor(){
        return new WLoginInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册后台系统登录拦截器
        InterceptorRegistration loginRegistration = registry.addInterceptor(getLoginInterceptor());
        loginRegistration.addPathPatterns("/service/**","/admin/**","/decorators/main")
            .excludePathPatterns("/service/login");
        //注册过磅软件登录拦截器
        InterceptorRegistration wsloginRegistration = registry.addInterceptor(getWLoginInterceptor());
        wsloginRegistration.addPathPatterns("/wapi/**")
            .excludePathPatterns("/wapi/login/**");
    }
}
